package org.gmr;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.CompletableFuture;


/**
 * 香港电池项目工厂添加模拟数据
 */

public class Test2 {

    public final static String url = "http://123.15.47.158:8850/ccp-ecmp-meter-data-service/web-api/meter-data/mock/v1/update";

    private static final String authorization = "bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJudW1iZXIiOiI2ODI0NGUxNzQ2ZTBmYjAwMDc2MTI2M2IiLCJlbmhhbmNlT3JnYW5pemF0aW9uSW5mbyI6eyJyb290T3JnYW5pemF0aW9uSWQiOiJyb290X29yZ2FuaXphdGlvbl9yb290X3RlbmFudF9pZCIsInJvb3RPcmdhbml6YXRpb25OYW1lIjoi5Zu95py65LqS6IGUSURBQVMiLCJwYXJlbnRPcmdhbml6YXRpb25JZCI6IjYzN2M3OWRiNDZlMGZiMDAwN2VmNzIyZSIsImV4dGVybmFsUGFyZW50T3JnYW5pemF0aW9uSWQiOiI2MzdjNzlkYjQ2ZTBmYjAwMDdlZjcyMmYiLCJwYXJlbnRPcmdhbml6YXRpb25OYW1lIjoi5bmz5Y-w6L-Q6JCl6LSm5oi3IiwicGFyZW50T3JnYW5pemF0aW9uRnVsbFBhdGgiOiJyb290X29yZ2FuaXphdGlvbl9yb290X3RlbmFudF9pZC82MzdjNzlkYjQ2ZTBmYjAwMDdlZjcyMmUiLCJkZXBhcnRtZW50SWQiOm51bGwsImV4dGVybmFsRGVwYXJ0bWVudElkIjpudWxsLCJkZXBhcnRtZW50TmFtZSI6bnVsbCwicm9vdERlcGFydG1lbnRJZCI6bnVsbCwicm9vdEV4dGVybmFsRGVwYXJ0bWVudElkIjpudWxsLCJyb290RGVwYXJ0bWVudE5hbWUiOm51bGx9LCJ1c2VyX25hbWUiOiJzaWlyaVRlc3QiLCJzY29wZSI6WyJy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U0VfTU9EVUxFX1BFUkZPUk1BTkNFX09VVFBVVCIsIk9QRVJBVElPTl9NT0RVTEVfUFJPRFVDVF9PVVRQVVQiLCJFTlRFUlBSSVNFX01PRFVMRV9FTkVSR1lfQ09OU1VNUFRJT05fQU5BTFlTSVMiXSwianRpIjoiZmU2YzhkNDktZTM5MS00M2U3LTgyYzgtYjlkMjMwOTRiOGZhIiwiY2xpZW50X2lkIjoiMTM3OWU0NDQtZTIyMy00N2NmLThmNjgtZjJkNzczZjRjYTI0In0.Pd4fUyE5d1hXcQibudwl0yioYOXo-xvplXNwyKZo0gGmXkY6XBsPzGZh-O5Y3kjEAnQTxsaSF2oPGmF8NMeeRgQSINGV_6xkWZ1cYgXAI4uDp6RRiIfPdLQgtrwc1urkykG1d9hRJWuu6u2YuouhB4gcpXEqXGJkVOmoAkzJCS2DiMaMI1NDq0FKOeyMRIKYfsiGVu87CgLK10gzNvPdoQ2Ef3zFvGTHK1OC7X1oYL-HFZCNXcxO3GIVqGLeT5ZwwR5MC5_CkIzlGrIa4bfb7uLJ76vNlT14Hv1v-hI59df52-2RZG9Z15Ga0dhp5Tn89yAelinLCNSf7HlD7No7KA";


    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        List<Meter> meters = getMeters();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.of(2024, 9, 1);
        LocalDate endDate = LocalDate.of(2025, 10, 30); // 9月有30天
        List<String> dateList = new ArrayList<>();
        LocalDate currentDate = startDate;
        while (!currentDate.isAfter(endDate)) {
            dateList.add(currentDate.format(formatter));
            currentDate = currentDate.plusDays(1);
        }

        List<CompletableFuture<Void>> completableFutureList = new ArrayList<>();
        for (Meter meter : meters) {
            CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> executeData(meter, dateList));
            completableFutureList.add(voidCompletableFuture);
        }

        CompletableFuture<Void> allOf = CompletableFuture.allOf(
                completableFutureList.get(0),
                completableFutureList.get(1),
                completableFutureList.get(2)
        );
        allOf.join();


        long end = System.currentTimeMillis();
        System.out.println(Thread.currentThread().getName() + ":所有异步任务都已经执行完毕, 耗时" + (end - start) / 1000 + "s");
    }

    private static void executeData(Meter meter, List<String> dateList) {
        Arrays.stream(meter.eleMeters).forEach(m -> {
            int value = meter.randomRange[2];
            for (String s : dateList) {
                Map<String, Object> data = new HashMap<>() {{
                    put("meterUuid", m);
                }};

                List<Object> valueList = new ArrayList<>();

                Map<String, Object> childData = new HashMap<>();
                childData.put("date", s);

                List<Integer> dayUsed = new ArrayList<>();

                for (int j = 0; j < 24; j++) {
                    dayUsed.add(value);
                    value += RandomUtil.randomInt(meter.randomRange[0], meter.randomRange[1]);

                }
                childData.put("valueList", dayUsed);

                valueList.add(childData);
                data.put("valueList", valueList);

                String jsonStr = JSONUtil.toJsonStr(data);
                System.out.println(Thread.currentThread().getName() + jsonStr);


                HttpResponse response = HttpRequest.post(url)
                        .header("Authorization", authorization)
                        .body(jsonStr)
                        .execute();

                if (!response.isOk()) {
                    System.out.println("Http request error!!!");
                }

                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }


        });

    }

    private static List<Meter> getMeters() {
        String[] eleMeters = {"68d4a03af4428a00019bffec", "68d4a048f4428a00019c0006", "68d4a058f4428a00019c0007", "68d4a071f4428a00019c0008", "68d4a091f4428a00019c0018"};
        String[] waterMeter = {"68d4a179f4428a00019c001f", "68d4a18af4428a00019c0020", "68d4a19ef4428a00019c0021", "68d4a1cbf4428a00019c002f"};
        String[] gasMeter = {"68d4a1ecf4428a00019c0037", "68d4a205f4428a00019c0038"};

        // start, end, rawVal
        int[] eleRandom = {0, 20, 0};
        int[] waterRandom = {0, 3, 0};
        int[] gasRandom = {0, 5, 0};

        Meter m1 = new Meter(eleMeters, eleRandom);
        Meter m2 = new Meter(waterMeter, waterRandom);
        Meter m3 = new Meter(gasMeter, gasRandom);

        return List.of(m1, m2, m3);
    }

}


class Meter {

    String[] eleMeters;
    int[] randomRange;

    public Meter(String[] eleMeters, int[] randomRange) {
        this.eleMeters = eleMeters;
        this.randomRange = randomRange;
    }
}
